package com.bilibili.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bilibili.entity.CommenResult;
import com.bilibili.entity.User;
import com.bilibili.entity.Video;
import com.bilibili.entity.VideoCollection;
import com.bilibili.service.SelectVideoService;
import com.bilibili.service.VideoCollectService;
import com.bilibili.service.impl.SelectVideoServiceImpl;
import com.bilibili.service.impl.VideoCollectServiceImpl;

@WebServlet("/updateVideoCount")
public class UpdateVideoCountServlet extends HttpServlet {
	
	private SelectVideoService sv= new SelectVideoServiceImpl();
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("application/json;charset=utf-8");
		String videoId=request.getParameter("videoId");//视频id
		String viewcount=request.getParameter("videwCount");//视频播放量
		String likecount=request.getParameter("likeCount");//视频点赞量
		String unlikecount=request.getParameter("unlikeCount");
		String coincount=request.getParameter("coinCount");// 视频投币量
		String collectcount=request.getParameter("collectCount");// 视频收藏量
		String uncollectcount=request.getParameter("uncollectCount");
		String clickcount=request.getParameter("clickCount");// 用户点击次数
		int video_id = Integer.parseInt(videoId);
		int clickCount=Integer.parseInt(clickcount);
		int viewCount=-1;
		int likeCount=-1;
		int unlikeCount=-1;
		int coinCount=-1;
		int collectCount=-1;
		int uncollectCount=-1;
		if(viewcount != null) {
			viewCount=Integer.parseInt(viewcount);
		}
		
		if(likecount != null) {
			if(likecount.contains(".999")) {
				likeCount=(int)(Double.parseDouble(likecount)+1);
			}else {
				likeCount=(int)Double.parseDouble(likecount);
			}
		}
		
		if(unlikecount != null) {
			if(unlikecount.contains(".999")) {
				unlikeCount=(int)(Double.parseDouble(unlikecount)-1);
			}else {
				unlikeCount=(int)Double.parseDouble(unlikecount);
			}
		}
		
		if(coincount != null) {
			coinCount=Integer.parseInt(coincount);
		}
		
		if(collectcount != null) {
			collectCount=Integer.parseInt(collectcount);
		}
		
		if(uncollectcount != null) {
			if(uncollectcount.contains(".999")) {
				uncollectCount=(int)(Double.parseDouble(uncollectcount)-1);
			}else {
				uncollectCount=(int)Double.parseDouble(uncollectcount);
			}
		}
		Video video=new Video();
		video.setVideoId(video_id);
		if(clickCount == 1) {
			video.setViewCount(viewCount+1);
			video.setLikeCount(likeCount+1);
			video.setCoinCount(coinCount+1);
			video.setCollectCount(collectCount+1);
		}else if(clickCount == 2) {
			video.setViewCount(viewCount+1);
			video.setLikeCount(likeCount-1);
			video.setCoinCount(coinCount+1);
			video.setCollectCount(collectCount-1);
		}
		
		VideoCollection vc = new VideoCollection();
		vc.setUserId(((User)request.getSession().getAttribute("loginUser")).getUserId());
		vc.setVideoId(video_id);
		
		VideoCollectService vcs = new VideoCollectServiceImpl();
		String action = request.getParameter("action");
		
		VideoCollection videoCollection = vcs.selectVideoCollect(vc);
		VideoCollection vc1 = videoCollection;
		CommenResult cr = new CommenResult();
		if(videoCollection != null) {
			if(action.equals("cancelClickFavorite")) {
				vcs.deleteVideoCollect(videoCollection.getCollectionRecordId());
				cr.setCode(409);
			}
		}else {
			if(action.equals("clickFavorite")) {
				vcs.insertVideoCollect(vc);
				cr.setCode(200);
			}
		}
		sv.updateAllCount(video);
		response.getWriter().write(cr.toJOSNString());
	}
}
